var divmaskdialog = $("<div class='div_mask_dialog'></div>");

//全局的ajax访问，处理ajax清求时sesion超时  
$.ajaxSetup({   
    contentType:"application/x-www-form-urlencoded;charset=utf-8",   
    complete:function(XMLHttpRequest,textStatus){
      var sessionstatus=XMLHttpRequest.getResponseHeader("SESSION_TIME_OUT"); //通过XMLHttpRequest取得响应头，sessionstatus，  
      if(sessionstatus=="timeout"){   
    	  //如果超时就处理 ，指定要跳转的页面  
    	  $("#divShowMessasgeBtnOk").click();
    	  openAlert("Session 过期, 请重新登陆",function(){    		  
    		  window.location.replace("${ctx}/login.do");  
    	  });
    	  return false;
      }   
       }   
});   
 

function openAlert(message,callback) {
	$("body").append(divmaskdialog);
	divmaskdialog.show();
    var divContent = "<div id='alert_dialog' class='div-dialog-modal'>"
    							+"<div class='modal-header'> <h3>提示信息</h3> </div>"
    							+"<div class='modal-body'  id='divShowMessasgeContent' >"+message+"</div>"
    							+"<div class='modal-footer'>"
    							+"<div class='toolButton' id='divShowMessasgeBtnOk' >确定</div></div>"//onclick='confirmButton(2)'
    							+"</div>";
    $("body").append(divContent);
    $("#divShowMessasgeBtnOk").bind('click',function(){
    	$("#alert_dialog").remove();
    	divmaskdialog.hide();
    	if(callback && typeof(callback)=='function'){
    		callback();
    	}
    });
    $("#alert_dialog").show();
}

function confirmButton(id){
	if(id == 1){
		$("#confirm_dialog").remove();
	}else{
		$("#alert_dialog").remove();
	}
	divmaskdialog.hide();
}

function openConfirm(message,o, functionName, para) {
	$("body").append(divmaskdialog);
	divmaskdialog.show();
    var divContent = "<div id='confirm_dialog' class='div-dialog-modal'>"
    							+"<div class='modal-header'> <h3>提示信息</h3> </div>"
    							+"<div class='modal-body'  id='divShowMessasgeContent' >"+message+"</div>"
    							+"<div class='modal-footer'>"
    							+"<div class='toolButton' id='divShowMessasgeBtnYes'>是</div>" 
    							+"<div class='toolButton' id='divShowMessasgeBtnNo' onclick='confirmButton(1)'>否</div></div>"
    							+"</div>";
    $("body").append(divContent);
    $("#confirm_dialog").show();
    
    $("#divShowMessasgeBtnYes").click(function(){
    	o = o == undefined ? window : Object(o);//obj可以是js基本类型 
    	para = para == undefined ? window : Object(para);//obj可以是js基本类型
    	functionName.apply(o, para);  	
    	$("#confirm_dialog").remove();
    	divmaskdialog.hide();
    });
}

function popUpPanel(url,param,w,h){
	var left = (window.screen.width-w)/2;
	var top = (window.screen.height-h)/2;	
	var windowPara = "dialogHeight: "+h+"px;dialogWidth:"+w+"px;dialogTop:"+top+"px;dialogLeft:"+left+"px;";
	return  window.showModalDialog(url,param,windowPara);
}

function deleteArrayByIndex(index,array){
	if(index != null){
		for(var i = index;i<array.length;i++){
				array[i] = array[i+1];		
		}
		array.length = array.length-1;
	}
	return array;
}


var chooseIds = new Array;
var chooseId;
function chooseRow(o,isMultipSelect){
	var id = $(o).attr("id");
	
	if(isMultipSelect==false){		
			$("#"+chooseId).removeClass("list_choosedRow");			
		if(chooseId!=id){
			$("#"+id).addClass("list_choosedRow");
			chooseId = id;
		}else{
			chooseId = null;
		}
	}
	
	if(isMultipSelect==true){
		var flag = false;
		var index = null;
		if(chooseIds.length > 0){
			for(var i =0 ;i<chooseIds.length;i++){
				if(chooseIds[i] == id){
					index = i;
					flag = true;
					break;
				}
			}
		}
		if(flag){
			$(o).removeClass("list_choosedRow");		
			chooseIds = deleteArrayByIndex(index,chooseIds);
		}else{
			$(o).addClass("list_choosedRow");
			chooseIds.push(id);
		}
	}
}
function getSelectedRows(){
	if(chooseId)
		return chooseId;
	return chooseIds;
}

function getFormObj(formId){
	var rlt = {};	
	$("#"+formId+" input").each(function(){
		if($(this).val()!=null){
			rlt[$(this).attr("name")] =$(this).val().trim();	
			
		}else{
			rlt[$(this).attr("name")] =$(this).val();
		}
		
	});
	return rlt;
}

/**
 * 进行Html转义 
 * */
function switcHtml(str)   
{   
	if(str){
		var s = "";     
		s = str.replace(/&/g, "&amp;");   
		s = s.replace(/</g, "&lt;");   
		s = s.replace(/>/g, "&gt;");     
		s = s.replace(/\'/g, "&#39;");   
		s = s.replace(/\"/g, "&quot;");   
		s = s.replace(/\\n/g, "");
		s = s.replace(/\\r/g, "");
		s = s.replace(/\//g, "&#47;"); 
		return s;   
	}
	return "";
}  
/**
 * 进行Html反转
 * */
function switchHtmlReverse(str)   
{   
	if(str){
		var s = "";   
		s = str.replace(/&amp;/g, "&");   
		s = s.replace(/&lt;/g, "<");   
		s = s.replace(/&gt;/g, ">");     
		s = s.replace(/&#39;/g, "\'");   
		s = s.replace(/&quot;/g, "\"");   
		s = s.replace(/<br>/g, "\n");
		s = s.replace(/&#47;/g, "/"); 
		return s;   
	}
	return "";
}
/**
 * SessionStorage常用方法封装
 */
var SessionStorage = {
	/**
	 * 向sessionStorage中存储数据
	 * @param key
	 * @param value json格式
	 * @returns
	 */
	setItem: function(key, value){
		if(key && key!==null && key!=='undefined'){
			sessionStorage.setItem(key, JSON.stringify(value));
		}
	},
	/**
	 * 获取sessionStorage中的数据
	 * @param key
	 */
	getItem: function(key){
		if(key && key!==null && key!=='undefined'){
			var result = sessionStorage.getItem(key);
			if(result){
				return JSON.parse(result);
			}
		}
		return null;
	},
	/**
	 * 清空sessionStorage
	 * @returns
	 */
	clear: function(){
		sessionStorage.clear();
	},
	/**
	 * 设置查询参数到sessionStorage
	 * @param value
	 * 	searchParam:{
	 * 	    showCount: 20,
	 * 		currentPage: 1,
	 *      自定义
	 *  },
	 *  formParam:{
	 *  	自定义
	 *  }
	 */
	setSearchParam: function(value){
		this.setItem("pageParam", value);
	},
	/*
	 * 获取查询参数
	 */
	getSearchParam: function(){
		var result = this.getItem("pageParam");
		return result;
	}
};

/*
 * parameter 如果parameter中包含英文单引号或者双引号，则需要使用转义字符转义
 * return parameter 返回转换后的结构
 */
function convertSpecialChar( parameter )
{
	
	if( parameter != undefined && parameter != null && parameter != "" )
	{
		parameter = parameter.replace(/"/g, "\\\"").replace(/'/g, "\\\'");
	}
	return parameter;
}